import { OrganizationModel } from '@/pages/person/data';
import {createDept, updateDept} from '@/pages/person/service';
import { PlusOutlined } from '@ant-design/icons';
import {
  ActionType,
  ModalForm,
  ProFormSelect,
  // ProFormSwitch,
  ProFormText,
} from '@ant-design/pro-components';
import {  useRequest, FormattedMessage } from '@umijs/max';
import {  message, Button } from 'antd';
import React, {FC, useMemo, useRef, useState,} from 'react';
import {dataResponse, userResponse} from "@/pages/system/data";
import {queryUser} from "@/pages/system/service";
interface EditFormProps {
  reload?: ActionType['reload'];
  record?: any[];
  children?: React.ReactNode;
  value?: userResponse;
  roleList: dataResponse[]
}

const EditForm: FC<EditFormProps> = (props) => {
  const { reload, record, value, roleList } = props;
  const [messageApi, contextHolder] = message.useMessage();
  const formEl = useRef<HTMLFormElement>(null);
  const [] = useState()
  /* const intl = useIntl(); */
  const { run, loading } = useRequest( value ? updateDept : createDept, {
    manual: true,
    onSuccess: () => {
      messageApi.success('操作成功');
      reload?.();
    },
    onError: () => {
      messageApi.error('操作失败');
    },
  });
  const getUserquery = async () => {
    const res = await queryUser({username: value?.username})
    console.log(res)
    formEl?.current?.setFieldsValue({
      username: res?.result?.username,
      roleCode: res?.result?.roleCode,
    })
  }
  const newRoleList = useMemo(() => {
    return roleList.map(item => {
      return { label:item.name, value: item.code }
    })
  }, [roleList])
  return (
    <>
      {contextHolder}
      <ModalForm
        formRef={formEl}
        // initialValues={editDefaultValue}
        title={!value? "新增用户" :"编辑用户"}
        trigger={
          !value? (
            <Button type="primary" icon={<PlusOutlined />}>
              <FormattedMessage id="pages.searchTable.new" defaultMessage="New" />
            </Button>
          ): (
            <a key="editable" onClick={getUserquery}>
              编辑
            </a>
          )
        }
        width="400px"
        modalProps={{ okButtonProps: { loading }, centered: true }}
        onFinish={async (value: OrganizationModel.organizationResponse) => {
          console.log(value)
          await run(value);
          return true;
        }}
      >
        <ProFormText
          rules={[
            {
              required: true,
              message: '用户名称',
            },
          ]}
          width="md"
          name="username"
          label="用户名称"
        />
        <ProFormSelect
          name="roleCode"
          label="角色"
          width="md"
          showSearch
          options={newRoleList}
          placeholder="请选择"
        />
        <ProFormText
          rules={!value?[
            {
              required: true,
              message: '初始密码',
            },
          ]:[]}
          width="md"
          name="password"
          label="初始密码"
        />
        {/*<ProFormSwitch name="switch" label="是否激活" width="md" />*/}
      </ModalForm>
    </>
  );
};

export default EditForm;
